'use strict';
const db = uniCloud.database()
const coll = db.collection('user')
const coll2 = db.collection('guanzhong')
const coll3 = db.collection('timu')
exports.main = async (event, context) => {
	if (event.isUser) {
		//选手登录
		let res = await coll.where({
			name: event.name,
			// area: event.area
		}).get()
		console.log('res', res)
		if (res.data.length > 0) {
			//如果题库存在 直接调取返回  不存在就保存

			if (res.data[0].tiku.length > 0) {
				return {
					status: 1,
					msg: '登陆成功',
					data: res.data[0],
					timuarr: res.data[0].tiku
				}
			} else {
				//匹配成功
				//获取该成员下的题目数组
				let timuRes = await coll3.where({
					area: res.data[0].teamname
				}).get()
				console.log('题目总数', timuRes.data)
				let allTimu = timuRes.data //题目总数
				//随机抽取5个返回客户端
				let result = []
				let ranNum = 5;
				for (let i = 0; i < ranNum; i++) {
					let ran = Math.floor(Math.random() * (allTimu.length - i));

					result.push(allTimu[ran]);

					allTimu[ran] = allTimu[allTimu.length - i - 1];

				};
				console.log('抽取的5题', result)
				//存入题库
				let resX = await coll.where({
					name: event.name,
					// area: event.area
				}).update({
					tiku: result
				})

				return {
					status: 1,
					msg: '登陆成功',
					data: res.data[0],
					timuarr: result
				}
			}


		} else {
			//登录失败
			return {
				status: 0,
				msg: '账号信息错误，请重试'
			}
		}


	} else {
		//匹配成功
		//获取该成员下的题目数组
		let timuRes = await coll3.get()
		console.log('题目总数', timuRes.data)
		let allTimu = timuRes.data //题目总数
		//随机抽取5个返回客户端
		let result = []
		let ranNum = 5;
		for (let i = 0; i < ranNum; i++) {
			let ran = Math.floor(Math.random() * (allTimu.length - i));

			result.push(allTimu[ran]);

			allTimu[ran] = allTimu[allTimu.length - i - 1];

		};
		console.log('观众抽取的5题', result)
		//观众登录
		//查询是否存在
		let res = await coll2.where({
			name: event.guanzhongname,
			tel: event.guanzhongtel
		}).get()

		//如果题库存在 直接调取返回  不存在就保存



		/* let res0 = await coll2.where({
			name: event.guanzhongname,
			tel: event.guanzhongtel
		}).update({
			tiku:result
		}) */
		if (res.data.length > 0) {
			if (res.data[0].tiku.length > 0) {
				return {
					status: 1,
					msg: '登陆成功',
					data: res.data[0],
					timuarr: res.data[0].tiku
				}
			} else {
				let res = await coll2.where({
					name: event.guanzhongname,
					tel: event.guanzhongtel
				}).update({
					tiku: result
				})
				return {
					status: 1,
					msg: '登陆成功',
					data: res.data[0],
					timuarr: result
				}
			}


			//存在直接登录  登陆成功
			/* return {
				status: 1,
				msg: '登陆成功',
				data: res.data[0],
				timuarr: result
			} */
		} else {
			//不存在 创建 再返回 初始化标识 为 guanzhong
			let res1 = await coll2.add({
				name: event.guanzhongname,
				tel: event.guanzhongtel,
				flag: 'guanzhong',
				zongfen: 0,
				shijian: 0,
				status: 0,
				tiku: result,
				tiNum: 1
			})
			console.log('res1', res1)
			let res2 = await coll2.where({
				_id: res1.id
			}).get()




			return {
				status: 1,
				msg: '登陆成功',
				data: res2.data[0],
				timuarr: res2.data[0].tiku
			}
		}


	}
};
